package com.mec.web.y2012.coreservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class GzipUtilities {

	public static boolean isGzipSupported(HttpServletRequest request){
		String encoding = request.getHeader("Accept-Encoding");
		return (null != encoding) && (encoding.indexOf("gzip") != -1);
	}
	
	public static boolean isGzipDisabled(HttpServletRequest request){
		String flag = request.getParameter("disableGzip");
		return  !((null == flag) || 
				(flag.equalsIgnoreCase("false")));
	}
	
	public static PrintWriter getGzipWriter(HttpServletResponse response) throws IOException, ServletException{
		return (new PrintWriter(new GZIPOutputStream(response.getOutputStream())));
	}
	
	private static final Logger logger = 
			Logger.getLogger(GzipUtilities.class);
}
